#
# ApplicationAndHelpData.py
#
# This module contains Google Datastore models for supporting
# application and user configuration as well as models supporting
# the application's help system.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Copyright 2010- Jeremy Nelson, Tomichi Informatics LLC
#
import basedata,datetime

from FRBRData import CorporateBody

from google.appengine.ext import db
from google.appengine.api import users


class Administrators(basedata.BaseModel):
    ''' Allowed to administor ecataloger users and
        collection information.'''
    user = db.UserProperty(required=True)
    

class AppInfo(basedata.BaseModel):
    ''' Basic configuration information.'''
    institution = db.ReferenceProperty(CorporateBody)
    app_name = db.StringProperty(default="ELibrary")
    start_ip_addr = db.StringProperty()
    end_ip_addr = db.StringProperty()

class Contributors(basedata.BaseModel):
    ''' Allowed contributors to ecataloger collection,
        initially limited to rating and tagging when logged
        in, could add more functionality in the future.'''
    user = db.UserProperty(required=True)

class Editors(basedata.BaseModel):
    ''' Allowed to change/edit any entities in the ecataloger
        collections. Automatically added to contributor model
        if added to editor.'''
    user = db.UserProperty(required=True)

    def put(self):
        query = Contributors.all().filter("user=",self.user)
        if not query.get():
            new_contributor = Contributors(user=self.user)
            new_contributor.put()
        return db.Model.put(self)

class HelpTopics(basedata.BaseModel):
    ''' Help topics to display to end user.'''
    answer = db.TextProperty(required=True)
    question = db.StringProperty(required=True)
    related_topics = db.ListProperty(db.Key,
                                     default=None)
    tags = db.ListProperty(db.Category,
                           default=None)
    type_of = db.StringProperty(choices=["howdoi",
                                         "general"],
                                default=None)
    usage = db.ListProperty(datetime.datetime,default=None)
    
# ---------------------------------------------------------------------------- #
# Helper Methods
# ---------------------------------------------------------------------------- #
def getUserRights(user):
    user_dict = {'admin':False,
                 'editor':False,
                 'contributor':False}
    def query_ds(class_name,user):
        query_str = '''SELECT __key__ FROM %s WHERE user=:1''' % class_name
        query = db.GqlQuery(query_str,user)
        if query.get():
            if class_name == 'Administrators':
                user_dict['admin'] = True
            elif class_name == 'Editors':
                user_dict['editor'] = True
            elif class_name == 'Contributors':
                user_dict['contributor'] = True
    query_ds('Administrators',user)
    query_ds('Editors',user)
    query_ds('Contributors',user)
    return user_dict

